package com.example.demo.controller;

import com.example.demo.model.Book;
import com.example.demo.model.NameAndValue;
import com.example.demo.service.BookFeignService;
import com.example.demo.service.UserFeignService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/12/14 10:17
 */
@Controller
public class TestController {
@Autowired
private BookFeignService bookFeignService;
    @Autowired
private ObjectMapper objectMapper;
    @PostMapping ("gethighFrequencyForString")
    public String test(@RequestParam("content")String content, @RequestParam("size") String size,Model model) throws JsonProcessingException {
        String json = bookFeignService.GethighFrequencyForString(content,Integer.parseInt(size) );
        HashMap<String, Float> stringIntegerHashMap = objectMapper.readValue(json, new TypeReference<HashMap<String, Float>>() {
        });
        ArrayList<NameAndValue> nameAndValues = new ArrayList<>();

        ArrayList<Integer> values = new ArrayList<>();

        for (Map.Entry<String, Float> stringIntegerEntry : stringIntegerHashMap.entrySet()) {
            nameAndValues.add(new NameAndValue(stringIntegerEntry.getKey(),stringIntegerEntry.getValue().intValue()));
            values.add(stringIntegerEntry.getValue().intValue());
            // System.out.println(stringIntegerEntry.getKey()+stringIntegerEntry.getValue());
        }
        Set<Map.Entry<String, Float>> entries = stringIntegerHashMap.entrySet();
        Set<String> strings = stringIntegerHashMap.keySet();

        model.addAttribute("strings",strings);
        model.addAttribute("values",values);
        model.addAttribute("frequency",nameAndValues);
        return "yun";
    }
    @PostMapping ("gethighFrequencyForNovel")
    public String test(@RequestParam("bookid")Integer bookid, @RequestParam("startsize") Integer startsize, @RequestParam("offsize") Integer offsize, @RequestParam("size") Integer size,Model model) throws JsonProcessingException {
        String json = bookFeignService.GethighFrequencyForNovel(bookid,startsize,offsize,size );
        HashMap<String, Float> stringIntegerHashMap = objectMapper.readValue(json, new TypeReference<HashMap<String, Float>>() {
        });
        ArrayList<NameAndValue> nameAndValues = new ArrayList<>();
        ArrayList<Integer> values = new ArrayList<>();

        for (Map.Entry<String, Float> stringIntegerEntry : stringIntegerHashMap.entrySet()) {
            nameAndValues.add(new NameAndValue(stringIntegerEntry.getKey(),stringIntegerEntry.getValue().intValue()));
            values.add(stringIntegerEntry.getValue().intValue());
           // System.out.println(stringIntegerEntry.getKey()+stringIntegerEntry.getValue());
        }
        Set<Map.Entry<String, Float>> entries = stringIntegerHashMap.entrySet();
        Set<String> strings = stringIntegerHashMap.keySet();

        model.addAttribute("strings",strings);
        model.addAttribute("values",values);
        model.addAttribute("frequency",nameAndValues);
        return "yun";
    }

    @ResponseBody
    @GetMapping("getlisttest")
    public String getzhu() throws IOException {
        List<Book> getlisttest = bookFeignService.getlisttest(3);
        return getlisttest.toString();
    }

}
